package com.odm.communitymanage.ui

import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.LoginRegisterCallback
import com.odm.communitymanage.core.net.ApiService
import com.odm.communitymanage.ui.DoorSafe.DoorCardListFragment
import com.odm.communitymanage.ui.ServicePhone.ServicePhoneFragment
import com.odm.communitymanage.ui.activity.ActivityListFragment
import com.odm.communitymanage.ui.health.HealthFragment
import com.odm.communitymanage.ui.help.HelpListFragment
import com.odm.communitymanage.ui.idle.IdleListFragment
import com.odm.communitymanage.ui.pay.ExpenseFragment
import com.odm.communitymanage.ui.repair.SubmitRepairFragment
import com.rxjava.rxlife.lifeOnMain
import kotlinx.android.synthetic.main.fragment_home.*
import okhttp3.HttpUrl
import rxhttp.wrapper.cookie.ICookieJar
import rxhttp.wrapper.param.RxHttp


/**
 * @description: 首页（功能列表页面）
 * @author: ODM
 * @date: 2020/4/11
 */
class HomeFragment : BaseFragment() {

    init {
        refreshCookieState()
        readCookie()
    }

    override fun initViews() {
        //宫格功能列表点击事件 注册
        initGridFunctionOnClickEvent()

        //小区名 设置
        tvCommunityName.text = SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_COMMUNITY_NAME , getString(R.string.unknown))

    }



    private fun initGridFunctionOnClickEvent(){
        rlDoorSafe.setOnClickListener {
            start(DoorCardListFragment())
        }
        rlIdle.setOnClickListener {
            start(IdleListFragment())
        }
        rlHealthManage.setOnClickListener {
            start(HealthFragment())
        }
        rlSeekHelp.setOnClickListener {
            start(HelpListFragment())
        }
        rlFix.setOnClickListener {
            start(SubmitRepairFragment())
        }
        rlServicePhone.setOnClickListener {
            start(ServicePhoneFragment())
        }
        rlCommunityActivity?.setOnClickListener {
            start(ActivityListFragment())
        }
        rlPay?.setOnClickListener {
            start(ExpenseFragment())
        }
    }

/*    override fun onBackPressedSupport(): Boolean {
            val dialog = CommonDialog(requireContext())
            //不设置标题默认没有标题
            dialog.setContentText("是否确认退出？")
            dialog.setOnCancelClickListener {
                dialog.dismiss()
            }
            dialog.setOnEnsureClickListener {
                dialog.dismiss()
                //退出App
                ActivityUtils.finishAllActivities()
            }
            dialog.show()

        return true
    }*/

    override fun shouldPopUpCloseDialog(): Boolean {
        return true;
    }

    override fun getLayoutId(): Int {
        return  R.layout.fragment_home
    }

    private fun readCookie(){
        val iCookieJar = RxHttp.getOkHttpClient().cookieJar() as ICookieJar
        val httpUrl = HttpUrl.parse("http://47.102.125.28:6666/")
        val cookies = iCookieJar.loadCookie(httpUrl) //读取到url对于的cookie
        SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_COOKIE_NAME , cookies.get(0).name())
        SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_COOKIE_VALUE , cookies.get(0).value())
        LogUtils.d(
            SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_COOKIE_NAME, "") + "=" +
                    SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_COOKIE_VALUE, "")
        )
    }

    /**
     * 获取Cookie保持登录状态
     */
    fun refreshCookieState(){
        RxHttp.get(
            ApiService.GET_CHECK_CODE +
                    SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_PHONE_NUMBER , "")
                    + "/12345")
            .asObject(LoginRegisterCallback::class.java)
            .lifeOnMain(this)
            .subscribe({
                if(it.code == 1){
                    //已注册了，直接调用登录请求,然后存储返回的小区信息和用户名,身份证件号
//                    SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_COMMUNITY_NAME , it.data.villageName)
                    SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_NAME , it.data.name)
                    SPUtils.getInstance(Constants.SP_USER).put(Constants.USER_ID_CARD , it.data.idcard)
                }
            },{
                ToastUtils.showLong(it.message)
                LogUtils.e(it.message)
            })
    }
}